package com.example.syyao.myapp.Personal;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.syyao.myapp.MainActivity;
import com.example.syyao.myapp.R;
import com.example.syyao.myapp.Response.Get_response;
import com.example.syyao.myapp.Sign.Sign_in;
import com.example.syyao.myapp.WebContact.JsonUtil;
import com.example.syyao.myapp.WebContact.WebAccessTools;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

public class changename extends SwipeBackActivity {

    private EditText myname;
    private TextView fanhui;
    private TextView save;
    private Get_response get_response;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting_changenickname);
        overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_right);

        myname = (EditText) findViewById(R.id.myname);
        fanhui = (TextView) findViewById(R.id.txt_back_fans);
        save = (TextView) findViewById(R.id.textView5_fans);
        Intent intent=getIntent();
        String name=intent.getStringExtra("nickname");
        myname.setText(name);
        fanhui.setOnClickListener(new btn_fanhui());
        save.setOnClickListener(new btn_save());
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0,R.anim.slide_out_right);
    }

    public class btn_fanhui implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            finish();
        }
    }

    public class btn_save implements View.OnClickListener{


        @Override
        public void onClick(View v) {
            new Thread(new Runnable() {

                @Override
                public void run() {
                    SharedPreferences sharedPreferences = getSharedPreferences(MainActivity.PREFERENCE_NAME,MainActivity.MODE);
                    SharedPreferences.Editor editor=sharedPreferences.edit();
                    final String username = sharedPreferences.getString("username","1");
                    String webContent = WebAccessTools.changename(username,myname.getText().toString());
                    System.out.println(webContent);
                    get_response= (Get_response) JsonUtil.jsonToObject(webContent,
                            Get_response.class);
                    handler.sendEmptyMessage(0x123);
                }
            }).start();
        }
    }

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (get_response != null && get_response.getMessage().equals("成功")) {
                Toast.makeText(changename.this, "修改成功！",Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(changename.this,MainActivity.class);
                startActivity(intent);
            }
        };
    };
}
